package com.itheima.scheduled;

import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Set;

@Component
@Slf4j
public class PicScheduled {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;

    @Transactional
    @Scheduled(cron = "0 0/5 * * * ? ")   //每5分钟执行一次
    public void execute() {
        Set<String> set = redisTemplate.opsForSet().difference("setmealPicDbUrl", "setmealPicDbUrl2");
        log.info("set:{}", set);
        if (set != null && !set.isEmpty()) {
            set.forEach(key -> {
                int lastIndex = key.lastIndexOf('/');

                redisTemplate.opsForSet().remove("setmealPicDbUrl", key);
                aliOssUtil.delete(key.substring(lastIndex + 1));

                log.info("删除图片：{}", key.substring(lastIndex + 1));
            });
        }

    }


}
